package crp.infra.test.utils;

import com.qwlabs.lang.C2;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;

import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;

public final class Randoms {
    private Randoms() {
    }

    public static String randomCode() {
        return "code" + RandomStringUtils.randomNumeric(RandomUtils.nextInt(3, 10));
    }

    public static String randomString() {
        return RandomStringUtils.randomAlphabetic(3, 10);
    }

    public static <E extends Enum<E>> E randomEnum(Class<E> enumClass) {
        return randomEnum(enumClass, false);
    }

    public static <E extends Enum<E>> E randomEnum(Class<E> enumClass, boolean withNull) {
        Set<E> values = EnumSet.allOf(enumClass);
        if (withNull && RandomUtils.nextDouble(0, 1) <= (1.0d / values.size())) {
            return null;
        }
        return random(values);
    }

    public static <E> E random(Collection<E> c) {
        return C2.mapNotEmpty(c, () -> {
            List<E> list = C2.list(c);
            return list.get(RandomUtils.nextInt(0, list.size()));
        }).orElse(null);
    }
}
